package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.bean.Question;
import com.chocho.producerserver.bean.Test;
import com.chocho.producerserver.dao.QuestionDao;
import com.chocho.producerserver.dao.TestDao;
import com.chocho.producerserver.utils.JwtUtil;
import com.chocho.producerserver.utils.UUIDUtil;
import com.chocho.producerserver.vo.VCollection;
import com.chocho.producerserver.vo.VQuestion;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/11.
 */
@Service
public class QuestionService {

    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private TestDao testDao;

    public boolean addQuestion(HttpServletRequest request, VQuestion v){
        Question question = new Question();
        question.setQuestionId(UUIDUtil.getUUID());
        int num = questionDao.getMaxNum() + 1;
        question.setQuestionNum(num);
        question.setQuestionTitle(v.getTitle());
        question.setQuestionDoNum(0);
        question.setQuestionPassNum(0);
        question.setQuestionScore(v.getScore());
        question.setQuestionContext(v.getContext());
        question.setTypeId(v.getTypeId());
        question.setQcollectionId(v.getQcollectionId());
        question.setQuestionDifficlut(v.getDifficult());

        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            question.setAuthor(claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }

        question.setKnowledgeNode(v.getKnowledgeNode());
        question.setTimeLimit(v.getTimeLimit());
        question.setMemoryLimit(v.getMemoryLimit());
        question.setQuestionAnswerOne(v.getAnswer1() == null ? "" : v.getAnswer1());
        question.setQuestionAnswerTwo(v.getAnswer2() == null ? "" : v.getAnswer2());
        question.setQuestionAnswerThree(v.getAnswer3() == null ? "" : v.getAnswer3());

        return questionDao.insert(question) > 0;
    }

    public List<VQuestion> getQuestionByQuery(Map<String, Object> map){
        List<Question> list = questionDao.getQuestionByQuery(map);
        List<VQuestion> questionList = new ArrayList<>();
        for(Question q : list){
            VQuestion v = new VQuestion();
            v.setId(q.getQuestionId());
            v.setNum(q.getQuestionNum());
            v.setTitle(q.getQuestionTitle());
            v.setDoNum(q.getQuestionDoNum());
            v.setPassNum(q.getQuestionPassNum());
            v.setScore(q.getQuestionScore());
            v.setContext(q.getQuestionContext());
            v.setTypeId("1".equals(q.getTypeId()) ? "选择题"
                    : "2".equals(q.getTypeId()) ? "填空题"
                    : "编程题");
            v.setQcollectionId(q.getQcollectionId());
            v.setDifficult(q.getQuestionDifficlut());
            //TODO 后面需要替换
            v.setAuthor("chocho");
            v.setKnowledgeNode(q.getKnowledgeNode());
            v.setTimeLimit(q.getTimeLimit());
            v.setMemoryLimit(q.getMemoryLimit());
            v.setAnswer1(q.getQuestionAnswerOne());
            v.setAnswer2(q.getQuestionAnswerTwo());
            v.setAnswer3(q.getQuestionAnswerThree());

            questionList.add(v);
        }

        return questionList;
    }

    public List<VQuestion> getMyQuestionByQuery(HttpServletRequest request, Map<String, Object> map){
        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            map.put("author", claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        List<Question> list = questionDao.getQuestionByQuery(map);
        List<VQuestion> questionList = new ArrayList<>();
        for(Question q : list){
            VQuestion v = new VQuestion();
            v.setId(q.getQuestionId());
            v.setNum(q.getQuestionNum());
            v.setTitle(q.getQuestionTitle());
            v.setDoNum(q.getQuestionDoNum());
            v.setPassNum(q.getQuestionPassNum());
            v.setScore(q.getQuestionScore());
            v.setContext(q.getQuestionContext());
            v.setTypeId("1".equals(q.getTypeId()) ? "选择题"
                    : "2".equals(q.getTypeId()) ? "填空题"
                    : "编程题");
            v.setQcollectionId(q.getQcollectionId());
            v.setDifficult(q.getQuestionDifficlut());
            //TODO 后面需要替换
            v.setAuthor("chocho");
            v.setKnowledgeNode(q.getKnowledgeNode());
            v.setTimeLimit(q.getTimeLimit());
            v.setMemoryLimit(q.getMemoryLimit());
            v.setAnswer1(q.getQuestionAnswerOne());
            v.setAnswer2(q.getQuestionAnswerTwo());
            v.setAnswer3(q.getQuestionAnswerThree());

            questionList.add(v);
        }

        return questionList;
    }

    public boolean deleteQuestion(String ids){
        List<String> idList = new ArrayList<>();
        String []idArr = ids.split("&");
        for(int i = 0; i < idArr.length; i++){
            idList.add(idArr[i]);
        }

        return questionDao.delete(idList) > 0;
    }

    public boolean updateQuestion(HttpServletRequest request, VQuestion v){
        Question question = new Question();
        question.setQuestionId(v.getId());
        question.setQuestionNum(v.getNum());
        question.setQuestionTitle(v.getTitle());
        question.setQuestionDoNum(v.getDoNum());
        question.setQuestionPassNum(v.getPassNum());
        question.setQuestionScore(v.getScore());
        question.setQuestionContext(v.getContext());
        question.setTypeId(v.getTypeId());
        question.setQcollectionId(v.getQcollectionId());
        question.setQuestionDifficlut(v.getDifficult());

        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            question.setAuthor(claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }

        question.setKnowledgeNode(v.getKnowledgeNode());
        question.setTimeLimit(v.getTimeLimit());
        question.setMemoryLimit(v.getMemoryLimit());
        question.setQuestionAnswerOne(v.getAnswer1());
        question.setQuestionAnswerTwo(v.getAnswer2());
        question.setQuestionAnswerThree(v.getAnswer3());

        return questionDao.update(question) > 0;
    }

    public int count(){
        return questionDao.getMaxNum();
    }

    public boolean getStatus(String questionId){
        Map<String, Object> map = new HashMap<>();
        map.put("questionId", questionId);
        List<Test> testList = testDao.getTestByQuery(map);

        boolean res = true;
        if(testList.size() > 0){
            for(Test t : testList){
                  if(t.getTestStatus() == 0){
                      res = false;
                      break;
                  }
            }
        }else{
            return false;
        }

        return res;
    }
}
